# 字符串的编码
a = "你好"
b = a.encode("utf-8")
print(f"你好的编码是：{b}")
print("~~~~~~~~~~~~~~~~~~~~~~~")

# 字符串的解码
c = b.decode()
print(f"{b}的解码是:" + c)
print("~~~~~~~~~~~~~~~~~~~~~~~")

# 字符串的CRUD

# Create(创建)
str1 = "    hello"
str1 += ', world   '
print(str1)
print("~~~~~~~~~~~~~~~~~~~~~~~")

# Retrieve(检索)
# 根据索引获取字符
print(str1[4])
# find和index(获取目标字符的索引值)
print(str1.find("e"))
print(str1.index("l"))
print("~~~~~~~~~~~~~~~~~~~~~~~")

# UPDATE(更新)
# replace(替换)
str2 = str1.replace("wor", "wer")
print(str2)
# split(分割)
str3 = str2.split(",")
print(str3)
# join(拼接)
str4 = '*'.join(str3)
print(str4)
print("~~~~~~~~~~~~~~~~~~~~~~~")

# DELETE(删除)
# strip
str5 = str1.strip()
print(str5)

# 保存到文件
output = open("out.txt", "w", encoding="utf-8")
content = "hello"
output.write(content)
output.close()

# 读取文件
input = open("out.txt", "r", encoding="utf-8")
content = input.read()
print(content)

# 追加文件
output = open("output.txt", "a", encoding="utf-8")
content = "\nhello, world"
output.write(content)
output.close()
print("~~~~~~~~~~~~~~~~~~~~~~~")

# format
a1 = "ping"
b1 = "pong"
# 按传入参数默认顺序
print("play pingpong: {}, {}".format(a1, b1))
# 按指定参数索引
print("play pingpong: {0}, {1}, {0}, {1}".format(a1, b1))
# 按变量
print(f"playing pingpong: {a1}, {b1}")
# 小数的表示
print("{:.2f}".format(3.1415926))
# %
print("playing %s %s" % ("ping", "pong"))